import http from "@/utils/http";
import { bodyToQuery } from "@/utils/helper";
import type { ProduceplanAddType, ProduceOrderSearchType, ProduceOrderUpdateData, ProduceOrderDetailData, PaginationType } from "./params";
import type { ProducePlanListData, ProducePlanAddData, ProductionWorkOrderListData, ProductionWorkOrderData } from "./res";
import { headerCommon, headerMaster } from "../header";

// 标准写法
enum API {
    GET_PRODUCEPLAN_LIST_API = "/1.1/classes/productPlan", // 生产计划列表查询
    POST_PRODUCEPLAN_ADD_API = "/1.1/classes/productPlan", // 新增生产计划

    GET_WORK_ORDER_LIST_API = "/1.1/classes/productWorkOrder", // 生产工单列表查询
    PUT_WORK_ORDER_UPDATE_API = '/1.1/classes/productWorkOrder',
    GET_WORK_ORDER_DETAIL_API = '/1.1/classes/productWorkOrder'
}

// example
// 生产计划列表api
export const getProducePlanListApi = () =>
    http.get<any, ProducePlanListData>(API.GET_PRODUCEPLAN_LIST_API, {
        headers: headerCommon,
    });

// 新增生产计划api
export const postProducePlanAddApi = (data: ProduceplanAddType) =>
    http.post<any, ProducePlanAddData>(API.POST_PRODUCEPLAN_ADD_API, data, {
        headers: headerCommon,
    });

// 生产工单列表
export const getProduceWorkOrderApi = (data?: ProduceOrderSearchType, params?: PaginationType) =>
    http.get<any, ProductionWorkOrderListData>(API.GET_WORK_ORDER_LIST_API + `${data ? `?where=${JSON.stringify(data)}` : ""}`, {
        headers: headerCommon,
        ...(params ? { params: { limit: params.pageSize, skip: (params.currentPage - 1) * params.pageSize } } : {})
    })

// 更新工单状态
/**
 * 
 * @param data
 * @returns 
 */
export const putProduceWorkOrderUpdateApi = (data: ProduceOrderUpdateData) =>
    http.put<any, any>(API.PUT_WORK_ORDER_UPDATE_API + `/${data.objectId}`, data, {
        headers: headerMaster
    })

// 获取工单详情
export const getProduceWorkOrderDetailApi = (data: ProduceOrderDetailData) =>
    http.get<any, ProductionWorkOrderData>(API.GET_WORK_ORDER_DETAIL_API + `/${data.objectId}`, {
        headers: headerCommon,
    })
